package org.techmore.activity;

import java.util.Map;

import org.techmore.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class FootButtonActivity {
	
	public FootButtonActivity(){}
	
	public void adapterFootButton(View view, Map<SendTargetHandlerKey, SendTargetHandler> targetMap){
		
    	LinearLayout couponListButton = (LinearLayout) view.findViewById(R.id.couponListButton);
    	LinearLayout myCouponListButton = (LinearLayout) view.findViewById(R.id.myCouponListButton);
    	LinearLayout couponScanner = (LinearLayout) view.findViewById(R.id.couponScanner);
    	
    	final SendTargetHandler couponListHandler = targetMap.get(SendTargetHandlerKey.gotoCouponList);
    	if(couponListHandler.isSend){
    		couponListButton.setOnTouchListener(new View.OnTouchListener() {
    			@Override
    			public boolean onTouch(View v, MotionEvent event) {
    				// TODO Auto-generated method stub
    				Intent intent = new Intent();
    				intent.setClass(couponListHandler.localContext, couponListHandler.targetClass);
    				couponListHandler.localActivity.startActivity(intent);
    				couponListHandler.localActivity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//    				MyCouponListActivity.this.finish();
    				return false;
    			}
    		});
    	}
    	
    	final SendTargetHandler couponScannerHandler = targetMap.get(SendTargetHandlerKey.gotoCouponScanner);
    	if(couponScannerHandler.isSend){
	    	couponScanner.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(couponScannerHandler.localContext);
					if(preferences.getBoolean("isLogin",false)){
						Intent intent = new Intent();
						intent.setClass(couponScannerHandler.localContext, couponScannerHandler.targetClass);
						couponScannerHandler.localActivity.startActivity(intent);
						couponScannerHandler.localActivity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
					}else{
						couponScannerHandler.localActivity.startActivity(new Intent(couponScannerHandler.localContext, couponScannerHandler.targetClass));
					}
					return false;
				}
			});
    	}
    	
    	final SendTargetHandler myCouponListHandler = targetMap.get(SendTargetHandlerKey.gotoCouponScanner);
    	if(myCouponListHandler.isSend){
	    	myCouponListButton.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setClass(myCouponListHandler.localContext, myCouponListHandler.targetClass);
					myCouponListHandler.localActivity.startActivity(intent);
					myCouponListHandler.localActivity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
	//				localContext.finish();
					return false;
				}
			});
	    	}
		}
	
	public enum SendTargetHandlerKey{
		gotoCouponList,
		gotoCouponScanner,
		gotoMyCouponList,
		gotoNearCoupon;
	}
    
    public class SendTargetHandler{
    	public SendTargetHandler(boolean isSend, Activity localActivity,
				Context localContext, Class<?> targetClass){
    		this.isSend = isSend;
			this.localActivity = localActivity;
			this.localContext = localContext;
			this.targetClass = targetClass;
    	}
    	
		public boolean isSend = false;
    	public Activity localActivity = null;
    	public Context localContext = null;
    	public Class<?> targetClass = null;
    }

}
